'use strict';

/**
 * @ngdoc function
 * @name sfhtHaitaoCmsApp.controller:PageManagerCtrl
 * @description
 * # PageManagerCtrl
 * Controller of the sfhtHaitaoCmsApp
 */
angular.module('sfhtHaitaoCmsApp')
    .controller('PageManagerCtrl', function($scope, $route, $modal, $log, $location, PageStore, AcitivityStore, $injector) {
        var DEFAULT_PAGE_SIZE = 25;
        $scope.checkAll = false;
        $scope.pages = [];
        $scope.totalItems = 1;
        $scope.currentPage = 1;
        $scope.isOnlyShowMine = PageStore.get("isOnlyShowMine") ? PageStore.get("isOnlyShowMine") : false;
        $scope.periodOptions = PageStore.get("periodOptions") ? PageStore.get("periodOptions") : 'LAST_ONE_WEEK';
        $scope.pageType = PageStore.get("pageType") ? PageStore.get("pageType") : 0;
        $scope.searchKeywords = null;
        var pageService = $injector.get("PageService");
        var topicService = $injector.get("TopicService");

        /**
         * 根据设备类型查询
         * @param  {int} 设备类型，pc，h5还是app
         * @return {array}
         */
        $scope.changeDeviceType = function(pageType) {
            $scope.pageType = pageType;
            getPageList($scope.periodOptions, $scope.isOnlyShowMine, $scope.searchKeywords, $scope.currentPage, $scope.pageType);
        };

        $scope.search = function() {
            getPageList($scope.periodOptions, $scope.isOnlyShowMine, $scope.searchKeywords, $scope.currentPage, $scope.pageType);
        };

        $scope.init = function() {
            getPageList($scope.periodOptions, $scope.isOnlyShowMine, $scope.searchKeywords, 1, $scope.pageType)
            topicService.getSpecialPageList()
                .then(function(data) {
                    $scope.specialPages = data.value;
                });
        };
        $scope.pageNumberChanged = function() {
            getPageList($scope.periodOptions, $scope.isOnlyShowMine, $scope.searchKeywords, $scope.currentPage, $scope.pageType);
        };
        /**
         * 查询页面列表
         * @param  {Boolean} 是否只查询我编辑的页面
         * @param  {string} 搜索关键词
         * @param  {int} 分页的页码
         * @param  {int} 页面类型0:pc,1:h5
         * @return {array}
         */
        var getPageList = function(periodOptions, isOnlyShowMine, searchKeywords, pageIndex, pageType) {
            var pagedQueryOption = {
                pageIndex: pageIndex,
                pageSize: DEFAULT_PAGE_SIZE
            };
            PageStore.set("periodOptions", periodOptions);
            PageStore.set("isOnlyShowMine", isOnlyShowMine);
            PageStore.set("pageType", pageType);
            pageService.getPageList(periodOptions, isOnlyShowMine, searchKeywords, pageType, pagedQueryOption)
                .then(function(data) {
                    $scope.pages = data.cmsPageList
                    $scope.totalItems = data.pageInfo.totalCount;
                    $scope.currentPage = data.pageInfo.pageIndex
                });
        };

        /**
         * 把页面设置为频道
         * @param  {string} uri
         * @param  {string} 页面名称
         * @return {array}
         */
        $scope.pageRevision = function(type, uri, pageId) {
            if (window.confirm("确定保存?")) {
                if (window.confirm("警告:请再次确认!")) {
                    topicService.updateSepcialPage(type, uri, pageId)
                        .then(function(data) {
                            alert("修改成功！！");
                            $route.reload();
                            // window.location.reload(true);
                        }, function(data, code) {
                            alert("修改失败,code:" + code);
                        });
                }
            }
        };
        //搜索过滤
        $scope.isSearch = function(searchKeywords) {
            if (searchKeywords && searchKeywords.length > 0) {
                return searchKeywords;
            } else {
                return;
            }
        }

        //批量更新页面
        $scope.batchCreatePage = function(pages) {
            var pageIdList = "";
            _.each(pages, function(page) {
                if (page.checked) {
                    pageIdList = pageIdList + page.id + ",";
                }
            });
            if (pageIdList == "") {
                alert("请选中要更新的页面！！！");
                return;
            }

            pageService.batchRecreatePage(pageIdList)
                .then(function(data) {
                    if (data.value != null && data.value != "") {
                        alert("刷新失败的页面id：" + data.value);
                    } else {
                        alert("刷新成功！！");
                    }
                }, function(data) {
                    alert("刷新失败" + data);
                });
        };
        $scope.batchUpdateProductData = function(pages){
            var pageIdList = [];
            _.each(pages, function(page) {
                if (page.checked) {
                    pageIdList.push(page.id);
                }
            });
            if (!(pageIdList && pageIdList.length > 0)) {
                alert("请选中要更新的页面！！！");
                return;
            }

            pageService.batchUpdateProductData(pageIdList)
                .then(function(data) {
                    if (data.value) {
                        alert("更新成功！！");
                    } else {
                        alert("更新失败！！");
                    }
                }, function(data) {
                    alert("刷新失败" + data);
                });
        }
        //全选中
        $scope.checkAll = function(pages) {
            _.each(pages, function(page) {
                if ($scope.allChecked) {
                    page.checked = true;
                } else {
                    page.checked = false;
                }
            });
        };
    })
